package com.imay.live.fragment.dynamics;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.activity.dynamics.LabelFeedsDetailActivity;
import com.imay.live.adapter.DynamicsGridAdapter;
import com.imay.live.model.FeedModel;
import com.imay.live.view.recyclerview.GridVerticalDividerItemDecoration;
import com.imay.live.view.recyclerview.WrapContentGridLayoutManager;
import com.imay.live.view.refresh.RefreshView;
import com.imay.live.R;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * 格子动态
 * Created by chan on 2017/3/25.
 */

public class GridFragment extends DynamicsBaseFragment implements DynamicsRecall {
    private DynamicsGridAdapter mAdapter;

    public static GridFragment newInstance() {
        GridFragment fragment = new GridFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getParentFragment() instanceof DynamicsFragment) {
            ((DynamicsFragment) getParentFragment()).setGridRecall(this);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dynameics_grid;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initWidget();
        initListener();
    }

    /**
     * 初始化监听
     */
    private void initWidget() {
        mRecyclerView = $(R.id.rcv_dynamics_content);

        mRecyclerView.setLayoutManager(new WrapContentGridLayoutManager(getActivity(), 3));
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new DynamicsGridAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        /*mRecyclerView.addItemDecoration(new GridVerticalDividerItemDecoration.Builder(getActivity(), 0)
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.divider_size_4x)
                .build());
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .colorResId(R.color.divider_ff)
                .sizeResId(R.dimen.divider_size_4x)
                .build());*/

        mRefreshLayout = $(R.id.dynamics_trl_refresh);
        RefreshView headerView = new RefreshView(getActivity());
        mRefreshLayout.setHeaderView(headerView);
        mRefreshLayout.setBottomView(new LoadingView(getActivity()));
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableOverScroll(false);
        mAdapter.setEnableLoadMore(false);

        mRefreshLayout.addView(loadView, 0, new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        loadView.startLoading();
    }

    /**
     * 初始化监听
     */
    protected void initListener() {
        super.initListener();
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Object object = adapter.getItem(position);
                if (object instanceof FeedModel) {
                    LabelFeedsDetailActivity.start(getActivity(), getString(R.string.str_dynamics_suggest_detail_title), position, (ArrayList<FeedModel>) adapter.getData());
                }
            }
        });
    }


    @Override
    public void play() {

    }

    @Override
    public void stop() {

    }

    /**
     * 刷新数据
     *
     * @param list
     */
    @Override
    public void setDatas(List list) {
        if (mRecyclerView.getVisibility() == View.INVISIBLE) {
            mRecyclerView.setVisibility(View.VISIBLE);
        }
        loadView.stopLoading();
        if (list.isEmpty()) {
            loadView.loadError();
        }
        mAdapter.setNewData(list);
        isLoading = false;
    }

    /**
     * 加载数据
     *
     * @param list
     */
    @Override
    public void addDatas(List list) {
        if (mRecyclerView.getVisibility() == View.INVISIBLE) {
            mRecyclerView.setVisibility(View.VISIBLE);
        }
        mAdapter.addData(list);
        isLoading = false;
    }

    @Override
    public void replaceDatas(int position, List list) {

    }

    @Override
    public void scrollToPosition(int position) {
        WrapContentGridLayoutManager wrapContentGridLayoutManager = (WrapContentGridLayoutManager) mRecyclerView.getLayoutManager();
        wrapContentGridLayoutManager.scrollToPositionWithOffset(position, 0);
    }

    @Override
    public void scrolltoTop() {
        super.scrolltoTop();
    }

    @Override
    public void netError() {
        mRecyclerView.setVisibility(View.INVISIBLE);
        loadView.netError();
    }

    @Override
    public void refresh() {
        mAdapter.getData().clear();
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 完成刷新
     */
    @Override
    public void setRefreshFinish() {
        if (mRefreshLayout != null) {
            mIsRefreshing = false;
            mRefreshLayout.finishRefreshing();
        }
    }

    /**
     * 完成加载
     */
    @Override
    public void setLoadFinish() {
        if (mRefreshLayout != null) {
            mIsRefreshing = false;
            mRefreshLayout.finishLoadmore();
        }
    }

    @Override
    public void loadError(String string) {
        super.loadError(string);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Override
    public void appLayoutStatus(boolean applayoutHide) {

    }
}
